//
// Created by Administrator on 2025/3/18.
//

#include "ui_elf_game_elf_info.h"


void ui_elf_fresh_elf_image(char image[64]);


void ui_elf_fresh_intimate(uint8_t intimate,int8_t intimate_change);

void ui_elf_fresh_prop_list(prop_data_t *list, uint32_t count,uint8_t type,lv_obj_t* prev_obj);

void ui_elf_fresh_prop_bar(uint8_t index);


static void elf_game_elf_info_add_event_common(lv_obj_t *obj, elf_game_event_data_t *data);

static void ui_elf_game_elf_info_enter_key(lv_group_t *group, lv_obj_t *obj, elf_game_event_data_t *data);

static void ui_elf_fresh_prop_list_plus();




static void ui_elf_game_show_flutter();

static void ui_elf_game_start_flutter_timer();
static void ui_elf_game_del_flutter_timer();
// 开启飘字显示和隐藏定时器
static void ui_elf_game_start_flutter_show_and_hide_timer();
// 删除飘字显示和隐藏定时器
static void ui_elf_game_del_flutter_show_and_hide_timer();
// 暂停飘字显示和隐藏定时器
static void ui_elf_game_pause_flutter_show_and_hide_timer();

// 隐藏语料飘字
static void ui_elf_game_hide_flutter();



static void ui_elf_game_elf_info_focus_cb(lv_group_t *group){
    LOG_INFO_WRAPPER("start");
    lv_obj_t *current_obj = lv_group_get_focused(group);
    elf_game_event_data_t *event_data = lv_obj_get_user_data(current_obj);
    if(event_data){
        // 对象类型
        LOG_INFO_WRAPPER("obj_type:%d", event_data->obj_type);
        if(event_data->obj_type == UI_ELF_GAME_OBJ_TYPE_PROP_BAR_ITEM){
            //如果index=当前道具栏下标，则增加checked，否则取消checked
            if(event_data->index == elf_game_module.data->current_prop_index){
                lv_obj_add_state(current_obj, LV_STATE_CHECKED);
            }else{
                lv_obj_clear_state(current_obj, LV_STATE_CHECKED);
            }
        }
    }
}

static void ui_elf_game_elf_info_screen_event(lv_event_t *e){
    LOG_INFO_WRAPPER("start");
//    lv_group_send_data()
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t *target = lv_event_get_target(e);
    lv_group_t *group = lv_group_get_default();
    elf_game_event_data_t * event_data = (elf_game_event_data_t *)lv_event_get_user_data(e);
    if (event_code == LV_EVENT_KEY)
    {
        // 按键事件 获取按键值
        uint32_t lv_key = lv_event_get_key(e);
        switch (lv_key)
        {
            case LV_KEY_UP:
                LV_LOG_USER("=========== touched KeyUp");
//                ui_word_query_keyboard_left_key(group,target,event_data);
                break;
            case LV_KEY_DOWN:
                LV_LOG_USER("============ touched KeyDown");
//                ui_word_query_right_keyboard_key(group, target, event_data);
                break;
            case LV_KEY_LEFT:
                LV_LOG_USER("=========== touched KeyLeft");
                lv_group_focus_obj(event_data->prev_obj);
//                ui_word_query_keyboard_left_key(group,target,event_data);
//                lv_group_focus_prev(group);

                break;
            case LV_KEY_RIGHT:
                LV_LOG_USER("============ touched KeyRight");
                lv_group_focus_obj(event_data->next_obj);
//                ui_word_query_right_keyboard_key(group, target, event_data);
//                lv_group_focus_next(group);

                break;
            case LV_KEY_ENTER:
                LV_LOG_USER("============ touched KeyEnter");
                ui_elf_game_elf_info_enter_key(group,target,event_data);

                break;
            case LV_KEY_BACKSPACE:
                LV_LOG_USER("============ touched KeyBackspace");

                break;
            case LV_KEY_DEL:
                // 模拟删除字母
                LV_LOG_USER("============ touched keydel");
//                word_query_keyboard_del_key(group, target, event_data);
                break;
            case LV_KEY_SPACE:
                // 模拟输入字母
                LV_LOG_USER("============ touched space");
                break;
            case LV_KEY_ESC:
                LV_LOG_USER("============ touched ESC");
                break;
            default:
                break;
        }
    }
}

static void ui_elf_game_elf_info_enter_key(lv_group_t *group, lv_obj_t *obj, elf_game_event_data_t *data) {
    LOG_INFO_WRAPPER("start obj_type:%d",data->obj_type);
    if(data->obj_type == UI_ELF_GAME_OBJ_TYPE_PROP_BAR_ITEM){
        // 刷新道具列表
        elf_game_module.data->current_prop_index = data->index;
        ui_elf_fresh_prop_list_plus();
    }else if(data->obj_type == UI_ELF_GAME_OBJ_TYPE_PROP_ITEM){
        /**
         * 使用道具
         * 1.是否有使用道具提示框，如果有则隐藏，然后增加心情值，飘字，如果没有则走使用道具流程
         * 2.获取当前道具，判断道具数据是否>0
         */
        int prop_id = data->index;
        LOG_INFO_WRAPPER("prop_id:%d",prop_id);
        prop_data_t * prop_info = get_prop_info_by_id(prop_id);
        lv_obj_t *use_prop_prompt = common_ui_comp_get_child(elf_game_module.screen->screen,
                                                             UI_COMP_ELF_USE_PROP_PROMPT_PANEL);
        lv_obj_t *use_prop_prompt_label = lv_obj_get_child(use_prop_prompt, 0);
        if(!lv_obj_has_flag(use_prop_prompt, LV_OBJ_FLAG_HIDDEN)){
            lv_obj_add_flag(use_prop_prompt, LV_OBJ_FLAG_HIDDEN);
            char *prop_text = lv_label_get_text(use_prop_prompt_label);
            if(strcmp(prop_text,"你的金币不足哦\n"
                                 "先去闯关背单词赢取金币吧！")){
                // 显示语料飘字
                ui_elf_game_show_flutter();
                // 开个2s定时器取消飘字和显示心情值
                ui_elf_game_start_flutter_timer();
            }

        }else{

            if(prop_info){
                // 删除飘字定时器
                // 记录当前道具id
                elf_game_module.data->current_use_prop_id = prop_id;
                ui_elf_game_del_flutter_timer();
                // 暂停定时器
                ui_elf_game_pause_flutter_show_and_hide_timer();
                if(prop_info->count > 0){
                    elf_game_data_sync_controller();
//                    elf_game_use_prop_common_dao_controller(prop_info, 0);
                }else{
                    // 隐藏飘字，恢复心情值
                    ui_elf_game_hide_flutter();
                    ui_elf_fresh_mood(elf_game_module.data->mood, 0);

                    // 剩余道具=0 显示购买道具页面 保存当前界面

                    ui_elf_hide_current_screen();
                    ui_elf_buy_prop_tip_screen_show(&elf_game_module);

                }
            }
        }


    }else if(data->obj_type == UI_ELF_GAME_OBJ_TYPE_RETURN_PANEL)
    {
        LOG_INFO_WRAPPER("UI_ELF_GAME_OBJ_TYPE_RETURN_PANEL");

    }
}




void ui_elf_game_elf_info_screen_init(ui_elf_game_main_screen_t *screen){
    lv_coord_t screen_width = lv_disp_get_hor_res(NULL);
    lv_coord_t screen_height = lv_disp_get_ver_res(NULL);
    if(screen->screen == NULL){
        lv_obj_t **children = lv_mem_alloc(sizeof(lv_obj_t *) * UI_COMP_ELF_INFO_SCREEN_NUM);
        if(children == NULL){
            LV_LOG_ERROR("ui_WordSpellScreen_screen_init children malloc failed");
            return;
        }
        if(screen->group == NULL){
            screen->group = ui_set_indev_group(indev_keypad);
        }
        screen->screen = ui_create_default_screen(lv_scr_act());

        // 屏幕panel
        lv_obj_t *main_panel = ui_create_column_default_panel(screen->screen);

        // 飘字 "很开心，今天又是元气满满的一天"
        lv_obj_t *flutter_panel = ui_elf_flutter_panel(screen->screen,"很开心，今天又是元气满满的一天");
        children[UI_COMP_ELF_FLUTTER_PANEL] = flutter_panel;
        lv_obj_add_flag(flutter_panel, LV_OBJ_FLAG_HIDDEN);


        // top panel
        lv_obj_t *top_panel = ui_word_query_top_panel_component(main_panel);
        lv_obj_t *top_return_panel = lv_obj_get_child(top_panel, 1);
        lv_obj_set_style_border_side(top_return_panel, LV_BORDER_SIDE_BOTTOM,LV_STATE_FOCUSED);
        lv_obj_set_style_border_width(top_return_panel, 2, LV_STATE_FOCUSED);
        elf_game_event_data_t *top_panel_event_data = malloc_set_zero(sizeof(elf_game_event_data_t));
        top_panel_event_data->obj_type = UI_ELF_GAME_OBJ_TYPE_RETURN_PANEL;
        elf_game_elf_info_add_event_common(top_return_panel, top_panel_event_data);
        lv_coord_t top_panel_height = lv_obj_get_style_height(top_panel, 0);
        LOG_INFO_WRAPPER("top_panel_height: %d", top_panel_height);

        // middle panel row_flex
        lv_coord_t middle_panel_height = screen_height - top_panel_height;
        lv_obj_t *middle_panel = ui_create_row_panel(main_panel, screen_width,
                                                        middle_panel_height);

        // middle left panel col_flex
        lv_coord_t middle_left_panel_width = screen_width / 2;
        lv_obj_t *middle_left_panel = ui_create_column_panel(middle_panel, middle_left_panel_width, middle_panel_height);
        lv_obj_set_style_pad_row(middle_left_panel, 11, 0);
        lv_obj_set_flex_align(middle_left_panel, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START);

        lv_obj_t *money_panel = ui_create_without_flex_panel(middle_left_panel, LV_SIZE_CONTENT, 20);
        lv_obj_set_style_pad_left(money_panel, 34, 0);
        lv_obj_t *money_label = ui_create_default_label(money_panel, "金币数: 9999",CHINESE_FONT_16_LIGHT);
        children[UI_COMP_ELF_INFO_COIN_LABEL] = money_label;

        lv_obj_t *elf_panel = ui_create_default_without_flex_panel(middle_left_panel);
        lv_obj_set_style_pad_top(elf_panel, 7, 0);
        lv_obj_set_style_pad_left(elf_panel, 38, 0);
//        lv_obj_t *elf_image = ui_create_default_image_by_src(elf_panel, &elf_elf_img);
        lv_obj_t *elf_image = ui_create_default_image_by_src(elf_panel, &elf_elf_img2);
        children[UI_COMP_ELF_INFO_ELF_IMAGE] = elf_image;

        // 心情值
        lv_obj_t *mood_panel = ui_create_column_panel(middle_left_panel, LV_SIZE_CONTENT, 40);
        lv_obj_set_flex_align(mood_panel,LV_FLEX_ALIGN_START,LV_FLEX_ALIGN_START,LV_FLEX_ALIGN_START);
        lv_obj_set_style_pad_row(mood_panel, 4, 0);
        lv_obj_set_style_pad_left(mood_panel, 38, 0);
        lv_obj_t *mood_label = ui_create_default_label(mood_panel, "心情值: 99",CHINESE_FONT_16_LIGHT);
        children[UI_COMP_ELF_INFO_MOOD_LABEL] = mood_label;
//        lv_obj_t *mood_label = ui_create_default_label(mood_panel, "亲密值: 99",CHINESE_FONT_16_LIGHT);

        // 亲密值
        lv_obj_t *intimate_label = ui_create_default_label(mood_panel, "亲密值: 99", CHINESE_FONT_16_LIGHT);
        children[UI_COMP_ELF_INFO_INTIMATE_LABEL] = intimate_label;
//        lv_obj_t *intimate_label = ui_create_default_label(mood_panel, "心情值: 99", CHINESE_FONT_16_LIGHT);
//        lv_obj_set_style_border_width(middle_left_panel, 1, 0);
//        lv_obj_set_style_border_side(middle_left_panel, LV_BORDER_SIDE_FULL, 0);

        // middle right panel col_flex
        lv_coord_t middle_right_panel_width = screen_width - middle_left_panel_width;
        lv_obj_t *middle_right_panel = ui_create_column_panel(middle_panel, middle_right_panel_width, middle_panel_height);
        lv_obj_set_style_pad_row(middle_right_panel, 8, 0);
        lv_obj_set_flex_align(middle_right_panel, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START);
        lv_obj_set_style_pad_left(middle_right_panel, 4, 0);
        lv_obj_set_style_pad_top(middle_right_panel, 6, 0);
//        lv_obj_set_style_border_width(middle_right_panel, 1, 0);
//        lv_obj_set_style_border_side(middle_right_panel, LV_BORDER_SIDE_FULL, 0);
        // 道具栏 row_flex
        char *prop_name[] = {"食物", "玩具", "清洁"};
        lv_obj_t *prop_bar_panel = ui_create_row_default_panel(middle_right_panel);
        lv_obj_set_style_pad_column(prop_bar_panel, 10, 0);
        children[UI_COMP_ELF_PROP_BAR_PANEL] = prop_bar_panel;
        elf_game_event_data_t *event_data = NULL;
        // 在event_data中保存上一个和下一个word_list_child_panel
        lv_obj_t *prev_prop_bar_panel = NULL;
        uint8_t prop_bar_count = sizeof(prop_name) / sizeof(prop_name[0]);
        for (int i = 0; i < prop_bar_count; ++i) {
            // 道具栏子项 width:50 height:30 radius:6px
            lv_obj_t *prop_bar_item = ui_create_without_flex_panel(prop_bar_panel, 50, 30);
            // 道具栏子项bottom
//            lv_obj_set_style_pad_bottom(prop_bar_item,4,0);
            lv_obj_set_style_radius(prop_bar_item, 6, 0);
            lv_obj_set_style_border_side(prop_bar_item, LV_BORDER_SIDE_FULL, 0);
            lv_obj_set_style_border_width(prop_bar_item, 1, 0);
            lv_obj_set_style_bg_color(prop_bar_item, lv_color_black(), LV_STATE_CHECKED);
            lv_obj_t *prop_bar_item_panel = ui_create_without_flex_panel(prop_bar_item, LV_SIZE_CONTENT, 20);
            event_data = malloc_set_zero(sizeof(elf_game_event_data_t));
            event_data->index = i;
            event_data->obj_type = UI_ELF_GAME_OBJ_TYPE_PROP_BAR_ITEM;
            event_data->prev_obj = prev_prop_bar_panel;
            // 让前一个 panel 的 next 指向当前 panel
            if (prev_prop_bar_panel){
                elf_game_event_data_t *prev_event_data = lv_obj_get_user_data(prev_prop_bar_panel);
                if (prev_event_data) {
                    prev_event_data->next_obj = prop_bar_item_panel;
                    prev_event_data->extra_obj = prop_bar_item_panel;
                }
            }
            // 更新 prev 变量，为下一次循环做准备
            prev_prop_bar_panel = prop_bar_item_panel;
            elf_game_elf_info_add_event_common(prop_bar_item_panel, event_data);
            // 道具栏子项文本bottom
            lv_obj_set_style_pad_bottom(prop_bar_item_panel,4,0);
            lv_obj_set_style_bg_opa(prop_bar_item_panel, LV_OPA_0, 0);
            lv_obj_set_style_border_side(prop_bar_item_panel, LV_BORDER_SIDE_BOTTOM, LV_STATE_FOCUSED);
            lv_obj_set_style_border_width(prop_bar_item_panel, 1, LV_STATE_FOCUSED);
            lv_obj_set_style_border_color(prop_bar_item_panel,lv_color_white(),LV_STATE_CHECKED);
            lv_obj_t *prop_bar_item_label = ui_create_default_label(prop_bar_item_panel, prop_name[i], CHINESE_FONT_16_LIGHT);
            lv_obj_set_style_text_color(prop_bar_item_label, lv_color_white(), LV_STATE_CHECKED);
            if(i == 0){
                // return panel的next  current_obj的pre_obj
                event_data->prev_obj = top_return_panel;
                top_panel_event_data->next_obj = prop_bar_item_panel;
            }else if(i == prop_bar_count-1){
                event_data->next_obj = top_return_panel;
                event_data->extra_obj = prop_bar_item_panel;
            }
        }

        // 文字描述
        lv_obj_t *desc_panel = ui_create_without_flex_panel(middle_right_panel, LV_SIZE_CONTENT, 20);
        lv_obj_t *desc_label = ui_create_default_label(desc_panel, "请选择要使用的道具:", CHINESE_FONT_16_LIGHT);


        lv_obj_t *prop_list_panel = ui_create_column_default_panel(middle_right_panel);
        lv_obj_set_style_pad_top(prop_list_panel, 4, 0);
        lv_obj_set_style_pad_row(prop_list_panel, 14, 0);
        children[UI_COMP_ELF_INFO_PROP_LIST_PANEL] = prop_list_panel;



        // 使用道具提示框
        lv_obj_t *prop_dialog_panel = ui_prompt_dialog(screen->screen, "成功使用道具「糯米糍」", 318, 158,
                                                       LV_SIZE_CONTENT,CHINESE_FONT_16_LIGHT);
        children[UI_COMP_ELF_USE_PROP_PROMPT_PANEL] = prop_dialog_panel;
//        lv_obj_t *prop_dialog_panel = ui_prompt_dialog(screen->screen, "你的金币不足哦\n"
//                                                                       "先去闯关背单词赢取金币吧！", 318, 158,
//                                                       LV_SIZE_CONTENT,CHINESE_FONT_16_LIGHT);
        lv_obj_set_style_text_line_space(lv_obj_get_child(prop_dialog_panel, 0), 8,0);
        lv_obj_add_flag(prop_dialog_panel,LV_OBJ_FLAG_HIDDEN);

        lv_obj_add_event_cb(screen->screen,common_get_component_child_event_cb,COMMON_LV_EVENT_GET_COMP_CHILD,children);
        lv_obj_add_event_cb(screen->screen,common_del_component_child_event_cb,LV_EVENT_DELETE,children);

        lv_group_set_focus_cb(screen->group,ui_elf_game_elf_info_focus_cb);



    }

}

static void elf_game_elf_info_add_event_common(lv_obj_t *obj, elf_game_event_data_t *data) {
    lv_group_add_obj(elf_game_module.screen->group,obj);
    // 设置 user_data（存储 event_data）
    if(data){
        lv_obj_set_user_data(obj, data);
        lv_obj_add_event_cb(obj, common_del_event_data_event_cb, LV_EVENT_DELETE,
                            data);
    }
    // 删除事件时回调
    lv_obj_add_event_cb(obj, ui_elf_game_elf_info_screen_event, LV_EVENT_KEY,
                        data);
}

static void ui_elf_fresh_prop_list_plus() {
    lv_obj_t *prop_bar_panel = common_ui_comp_get_child(elf_game_module.screen->screen, UI_COMP_ELF_PROP_BAR_PANEL);
    uint8_t current_prop_index = elf_game_module.data->current_prop_index;
    lv_obj_t *prop_bar_item_panel = lv_obj_get_child(lv_obj_get_child(prop_bar_panel,current_prop_index),0);
    ui_elf_fresh_prop_list(elf_game_module.data->prop_list, elf_game_module.data->prop_count,
                           elf_game_module.data->current_prop_index,prop_bar_item_panel);
    // 刷新道具栏状态
    ui_elf_fresh_prop_bar(elf_game_module.data->current_prop_index);

}


void ui_refresh_elf_info_screen(elf_game_module_t *module){
    LOG_INFO_WRAPPER("start");
    /**
     * 金币值
     * 精灵图片
     * 心情值
     * 亲密值
     * 道具列表
     */
    ui_elf_fresh_coin(module->data->coin);
    ui_elf_fresh_elf_image(module->data->elf_image);
    ui_elf_fresh_mood(module->data->mood,0);
    ui_elf_fresh_intimate(module->data->intimate,0);
    ui_elf_fresh_prop_list_plus();


}

void ui_elf_fresh_prop_bar(uint8_t index) {
    // 获取道具栏父项
    lv_obj_t *prop_bar_panel = common_ui_comp_get_child(elf_game_module.screen->screen, UI_COMP_ELF_PROP_BAR_PANEL);
    uint32_t child_cnt = lv_obj_get_child_cnt(prop_bar_panel);
    for (int i = 0; i < child_cnt; ++i) {
        // 道具栏子项
        lv_obj_t *prop_bar_item = lv_obj_get_child(prop_bar_panel, i);
        lv_obj_t *prop_bar_item_panel = lv_obj_get_child(prop_bar_item,0);
        lv_obj_t *prop_bar_item_label = lv_obj_get_child(prop_bar_item_panel, 0);
        if(i == index){
            // 增加checked状态
            lv_obj_add_state(prop_bar_item, LV_STATE_CHECKED);
            lv_obj_add_state(prop_bar_item_label, LV_STATE_CHECKED);
        }else{
            // 移除checked状态
            lv_obj_clear_state(prop_bar_item, LV_STATE_CHECKED);
            lv_obj_clear_state(prop_bar_item_label, LV_STATE_CHECKED);
        }
    }
}

void ui_elf_fresh_prop_list(prop_data_t *list, uint32_t count,uint8_t type,lv_obj_t* prev_obj) {
    lv_obj_t *prop_list_panel = common_ui_comp_get_child(elf_game_module.screen->screen, UI_COMP_ELF_INFO_PROP_LIST_PANEL);
    lv_obj_clean(prop_list_panel);
    // 在event_data中保存上一个和下一个word_list_child_panel
    lv_obj_t *prev_prop_list_child_panel = NULL;
    elf_game_event_data_t *event_data = NULL;
    // 第一个type 标志位
    bool is_find_first_type = false;
    bool is_first_type = false;
    lv_obj_t *last_prop_list_item = NULL; // 用于记录最后一个满足条件的对象
    for (int i = 0; i < count; ++i) {
        // 根据类型查找
        if(list[i].type == type){
            // 判断当前是否是第一个type
            if(!is_find_first_type){
                is_find_first_type = true;
                is_first_type = true;
            }else{
                is_first_type = false;
            }
            // 道具列表子项 width:169 height:30
            lv_obj_t *prop_list_item = ui_create_without_flex_panel(prop_list_panel, 169, 30);
            lv_obj_set_style_pad_bottom(prop_list_item,4,0);
            lv_obj_set_style_border_width(prop_list_item, 1, 0);
            lv_obj_set_style_border_width(prop_list_item, 2, LV_STATE_FOCUSED);
            lv_obj_set_style_radius(prop_list_item, 6, 0);
            lv_obj_set_style_border_side(prop_list_item, LV_BORDER_SIDE_FULL, 0);

            lv_obj_t *prop_list_item_label = ui_create_default_label(prop_list_item, list[i].name,
                                                                     CHINESE_FONT_16_LIGHT);
            event_data = malloc_set_zero(sizeof(elf_game_event_data_t));
            event_data->index = list[i].id;
            event_data->prev_obj = prev_prop_list_child_panel;
            event_data->obj_type = UI_ELF_GAME_OBJ_TYPE_PROP_ITEM;
            // 让前一个 panel 的 next 指向当前 panel
            if (prev_prop_list_child_panel){
                elf_game_event_data_t *prev_event_data = lv_obj_get_user_data(prev_prop_list_child_panel);
                if (prev_event_data) {
                    prev_event_data->next_obj = prop_list_item;
                }
            }
            elf_game_elf_info_add_event_common(prop_list_item, event_data);
            // 更新 prev 变量，为下一次循环做准备
            prev_prop_list_child_panel = prop_list_item;
            if(is_first_type){
                event_data->prev_obj = prev_obj;
                elf_game_event_data_t *prev_event_data = lv_obj_get_user_data(prev_obj);
//                prev_event_data->next_obj = prop_list_item;
                lv_group_focus_obj(prop_list_item);
            }

            // **更新最后一个匹配的对象**
            last_prop_list_item = prop_list_item;


        }

    }

    if(last_prop_list_item){
        // 最后一个prop_item，该obj下的event_data的next要指向prev_obj的extra_obj
        event_data = lv_obj_get_user_data(last_prop_list_item);
        elf_game_event_data_t *prev_event_data = lv_obj_get_user_data(prev_obj);
        event_data->next_obj = prev_event_data->extra_obj;
    }
}

void ui_elf_fresh_intimate(uint8_t intimate,int8_t intimate_change) {
    lv_obj_t *intimate_label = common_ui_comp_get_child(elf_game_module.screen->screen, UI_COMP_ELF_INFO_INTIMATE_LABEL);
    if(intimate_label) {
        if(intimate_change > 0){
            lv_label_set_text_fmt(intimate_label, "亲密值: %d+%d", intimate,intimate_change);
        }else if(intimate_change < 0){
            lv_label_set_text_fmt(intimate_label, "亲密值: %d-%d", intimate,intimate_change);
        }else{
            lv_label_set_text_fmt(intimate_label, "亲密值: %d", intimate);
        }
    }
}

void ui_elf_fresh_mood(uint8_t mood,int8_t mood_change) {
    lv_obj_t *mood_label = common_ui_comp_get_child(elf_game_module.screen->screen, UI_COMP_ELF_INFO_MOOD_LABEL);
    if(mood_label) {
        if(mood_change > 0){
            lv_label_set_text_fmt(mood_label, "心情值: %d+%d", mood,mood_change);
        }else if(mood_change < 0){
            lv_label_set_text_fmt(mood_label, "心情值: %d-%d", mood,mood_change);
        }else{
            lv_label_set_text_fmt(mood_label, "心情值: %d", mood);
        }
    }
}

void ui_elf_fresh_elf_image(char* image) {
    lv_obj_t *elf_image = common_ui_comp_get_child(elf_game_module.screen->screen, UI_COMP_ELF_INFO_ELF_IMAGE);
//    if(elf_image){
//        lv_img_set_src(elf_image, image);
//    }
}

void ui_elf_fresh_coin(uint32_t coin) {
    lv_obj_t *coin_label = common_ui_comp_get_child(elf_game_module.screen->screen, UI_COMP_ELF_INFO_COIN_LABEL);
    if(coin_label){
        lv_label_set_text_fmt(coin_label, "金币数: %d", coin);
    }
}

// 展示使用道具提示框
void ui_show_use_prop_prompt(char* name,uint8_t label_type) {
    lv_obj_t *prompt_panel = common_ui_comp_get_child(elf_game_module.screen->screen, UI_COMP_ELF_USE_PROP_PROMPT_PANEL);
    lv_obj_t *label = lv_obj_get_child(prompt_panel, 0);
    if(label_type == 0){
        // 成功使用道具
        lv_label_set_text_fmt(label, "成功使用道具「%s」", name);
    }else if(label_type == 1){
        // 金币不足
        lv_label_set_text(label, "你的金币不足哦\n"
                                 "先去闯关背单词赢取金币吧！");
    }
    lv_obj_clear_flag(prompt_panel, LV_OBJ_FLAG_HIDDEN);
}

// 展示语料飘字
static void ui_elf_game_show_flutter() {
    //根据心情值获取飘字,然后显示飘字panel
    char *flutter_text = elf_game_get_mood_text(elf_game_module.data->mood);
    LOG_INFO_WRAPPER("flutter_text:%s",flutter_text);
    lv_obj_t *flutter_panel = common_ui_comp_get_child(elf_game_module.screen->screen, UI_COMP_ELF_FLUTTER_PANEL);
    lv_obj_t  *label_panel = lv_obj_get_child(flutter_panel, 0);
    if(label_panel){
        lv_obj_t *label = lv_obj_get_child(label_panel,0);
        lv_label_set_text(label, flutter_text);
    }

    lv_obj_clear_flag(flutter_panel, LV_OBJ_FLAG_HIDDEN);
}

// 隐藏语料飘字
static void ui_elf_game_hide_flutter(){
    lv_obj_t *flutter_panel = common_ui_comp_get_child(elf_game_module.screen->screen, UI_COMP_ELF_FLUTTER_PANEL);
    lv_obj_add_flag(flutter_panel, LV_OBJ_FLAG_HIDDEN);
}

void ui_elf_game_start_flutter_timer_cb(lv_timer_t *timer) {
    LOG_INFO_WRAPPER("start");
    elf_game_module_t *module = timer->user_data;
    ui_elf_game_hide_flutter();
    ui_elf_fresh_mood(module->data->mood, 0);
    lv_timer_del(timer);
    module->screen->flutter_timer = NULL;
}

// 开个2s定时器取消飘字和显示心情值
static void ui_elf_game_start_flutter_timer() {
    ui_elf_game_del_flutter_timer();
    elf_game_module.screen->flutter_timer = lv_timer_create(ui_elf_game_start_flutter_timer_cb, 2000, &elf_game_module);
}

// 删除飘字定时器
static void ui_elf_game_del_flutter_timer() {
    if(elf_game_module.screen->flutter_timer){
        lv_timer_del(elf_game_module.screen->flutter_timer);
        elf_game_module.screen->flutter_timer = NULL;
    }
}

void ui_elf_game_flutter_show_and_hide_timer_cb(lv_timer_t *timer) {
    int flutter_status = (int) timer->user_data;
    LOG_INFO_WRAPPER("flutter_status:%d",flutter_status);
    // 隐藏飘字 设置显示飘字，修改定时器时间
    if(flutter_status == UI_ELF_FLUTTER_HIDE_STATUS){
        // 使用道具飘字定时器不存在才删除
        if(!elf_game_module.screen->flutter_timer){
            ui_elf_game_hide_flutter();
        }
        timer->user_data = (void *) UI_ELF_FLUTTER_SHOW_STATUS;
        timer->period = UI_ELF_FLUTTER_SHOW_TIME;
    }else if(flutter_status == UI_ELF_FLUTTER_SHOW_STATUS){
        // 显示飘字 设置隐藏飘字，修改定时器时间
        ui_elf_game_show_flutter();
        timer->user_data = (void *) UI_ELF_FLUTTER_HIDE_STATUS;
        timer->period = UI_ELF_FLUTTER_HIDE_TIME;

    }
}

// 开启飘字显示和隐藏定时器
static void ui_elf_game_start_flutter_show_and_hide_timer(){
    ui_elf_game_del_flutter_show_and_hide_timer();
    elf_game_module.screen->flutter_show_and_hide_timer = lv_timer_create(ui_elf_game_flutter_show_and_hide_timer_cb,
                                                                          UI_ELF_FLUTTER_SHOW_TIME, (void *)UI_ELF_FLUTTER_SHOW_STATUS);
}

// 删除飘字显示和隐藏定时器
static void ui_elf_game_del_flutter_show_and_hide_timer(){
    if(elf_game_module.screen->flutter_show_and_hide_timer){
        lv_timer_del(elf_game_module.screen->flutter_show_and_hide_timer);
        elf_game_module.screen->flutter_show_and_hide_timer = NULL;
    }
}

// 暂停飘字显示和隐藏定时器
static void ui_elf_game_pause_flutter_show_and_hide_timer() {
    if (elf_game_module.screen->flutter_show_and_hide_timer) {
            // 停止定时器
            lv_timer_pause(elf_game_module.screen->flutter_show_and_hide_timer);
    }
}

// 恢复飘字显示和隐藏定时器
void ui_elf_game_resume_flutter_show_and_hide_timer() {
    if(elf_game_module.screen->flutter_show_and_hide_timer){
        lv_timer_resume(elf_game_module.screen->flutter_show_and_hide_timer);
        lv_timer_reset(elf_game_module.screen->flutter_show_and_hide_timer);
    }
}

void ui_elf_game_elf_info_screen_show(elf_game_module_t *module){
    LOG_INFO_WRAPPER("start");
    if(module){
        if(module->screen->screen == NULL){
            ui_elf_game_elf_info_screen_init(module->screen);
        }
    }
    ui_refresh_elf_info_screen(module);
    module->screen->current_page = ELF_GAME_ELF_INFO_SCREEN;
    ui_elf_game_start_flutter_show_and_hide_timer();
    module->screen->deinit_cb = ui_elf_game_elf_info_screen_deinit;
}
void ui_elf_game_elf_info_screen_deinit(){
    LOG_INFO_WRAPPER("start");
    ui_screen_group_deinit_common(&(elf_game_module.screen->screen), &(elf_game_module.screen->group));
    ui_elf_game_del_flutter_timer();
    ui_elf_game_del_flutter_show_and_hide_timer();
}
